 package ac.zlf.springmybatis.core.util;
 
 import com.fasterxml.jackson.annotation.JsonAutoDetect;
 import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
 import com.fasterxml.jackson.annotation.PropertyAccessor;
 import com.fasterxml.jackson.core.JsonEncoding;
 import com.fasterxml.jackson.core.JsonFactory;
 import com.fasterxml.jackson.core.JsonGenerationException;
 import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.type.TypeReference;
 import com.fasterxml.jackson.databind.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;
 import com.fasterxml.jackson.databind.ObjectWriter;
 import com.fasterxml.jackson.databind.type.TypeFactory;
 import java.io.IOException;
 import java.io.InputStream;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import javax.servlet.http.HttpServletResponse;
 import net.sf.json.JSONArray;
 import net.sf.json.JSONObject;
 import org.apache.log4j.Logger;
 import org.springframework.http.MediaType;
 
 public class JsonHelper
 {
   private static Logger logger = Logger.getLogger(JsonHelper.class.getName());
   
   public static void writeJsonData(Object object, HttpServletResponse response)
   {
     ObjectMapper mapper = new ObjectMapper();
     try
     {
       JsonGenerator jsonGenerator = mapper.getFactory().createGenerator(response.getOutputStream(), JsonEncoding.UTF8);
       
       mapper.writeValue(jsonGenerator, object);
     }
     catch (IOException ex)
     {
       logger.error("Could not write JSON: " + ex.getMessage(), ex);
       ex.printStackTrace();
     }
   }
   
   public static JsonEncoding getJsonEncoding(MediaType contentType)
   {
     if ((contentType != null) && (contentType.getCharSet() != null))
     {
       Charset charset = contentType.getCharSet();
       for (JsonEncoding encoding : JsonEncoding.values()) {
         if (charset.name().equals(encoding.getJavaName())) {
           return encoding;
         }
       }
     }
     return JsonEncoding.UTF8;
   }
   
   public static <T> String list2Json(List<T> objects, Class clazz)
   {
     ObjectMapper mapper = new ObjectMapper();
     mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
     ObjectWriter typedWriter = mapper.writerWithType(mapper.getTypeFactory().constructCollectionType(List.class, clazz));
     String json = null;
     try
     {
       json = typedWriter.writeValueAsString(objects);
     }
     catch (IOException e)
     {
       logger.error("Could not write JSON: " + e.getMessage(), e);
       e.printStackTrace();
     }
     return json;
   }
   
   public static String object2Json(Object object, Class clazz)
   {
     ObjectMapper mapper = new ObjectMapper();
     mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
     String json = null;
     try
     {
       json = mapper.writeValueAsString(object);
     }
     catch (IOException e)
     {
       logger.error("Could not write JSON: " + e.getMessage(), e);
       
       e.printStackTrace();
     }
     return json;
   }
   
   public static String serialize(Object object)
   {
     ObjectMapper mapper = new ObjectMapper();
     String json = null;
     try
     {
       json = mapper.writeValueAsString(object);
     }
     catch (JsonGenerationException e)
     {
       logger.error("JsonGenerationException when serialize object to json", e);
     }
     catch (JsonMappingException e)
     {
       logger.error("JsonMappingException when serialize object to json", e);
     }
     catch (IOException e)
     {
       logger.error("IOException when serialize object to json", e);
     }
     return json;
   }
   
   public static <T> T deserialize(String json, Class<T> clazz)
   {
     ObjectMapper mapper = new ObjectMapper();
     Object object = null;
     try
     {
       object = mapper.readValue(json, TypeFactory.rawClass(clazz));
     }
     catch (JsonParseException e)
     {
       logger.error("JsonParseException when serialize object to json", e);
     }
     catch (JsonMappingException e)
     {
       logger.error("JsonMappingException when serialize object to json", e);
     }
     catch (IOException e)
     {
       logger.error("IOException when serialize object to json", e);
     }
     return (T) object;
   }
   
   public static <T> T deserialize(String json, TypeReference<T> typeReference)
   {
     ObjectMapper mapper = new ObjectMapper();
     try
     {
       return mapper.readValue(json, typeReference);
     }
     catch (JsonParseException e)
     {
       logger.error("JsonParseException when deserialize json", e);
     }
     catch (JsonMappingException e)
     {
       logger.error("JsonMappingException when deserialize json", e);
     }
     catch (IOException e)
     {
       logger.error("IOException when deserialize json", e);
     }
     return null;
   }
   
   public static Object deserialize(InputStream inputStream, Class<?> clazz)
     throws IOException
   {
     ObjectMapper mapper = new ObjectMapper();
     return mapper.readValue(inputStream, clazz);
   }
   
   public static String array2json(Object object)
   {
     JSONArray jsonArray = JSONArray.fromObject(object);
     return jsonArray.toString();
   }
   
   public static Object json2Array(String json, Class valueClz)
   {
     JSONArray jsonArray = JSONArray.fromObject(json);
     return JSONArray.toArray(jsonArray, valueClz);
   }
   
   public static String collection2json(Object object)
   {
     JSONArray jsonArray = JSONArray.fromObject(object);
     return jsonArray.toString();
   }
   
   public static String map2json(Object object)
   {
     JSONObject jsonObject = JSONObject.fromObject(object);
     return jsonObject.toString();
   }
   
   public static Map json2Map(Object[] keyArray, String json, Class valueClz)
   {
     JSONObject jsonObject = JSONObject.fromObject(json);
     Map classMap = new HashMap();
     for (int i = 0; i < keyArray.length; i++) {
       classMap.put(keyArray[i], valueClz);
     }
     return (Map)JSONObject.toBean(jsonObject, Map.class, classMap);
   }
   
   public static String bean2json(Object object)
   {
     JSONObject jsonObject = JSONObject.fromObject(object);
     return jsonObject.toString();
   }
   
   public static Object json2Object(String json, Class beanClz)
   {
     return JSONObject.toBean(JSONObject.fromObject(json), beanClz);
   }
   
   public static String string2json(String key, String value)
   {
     JSONObject object = new JSONObject();
     object.put(key, value);
     return object.toString();
   }
   
   public static String json2String(String json, String key)
   {
     JSONObject jsonObject = JSONObject.fromObject(json);
     return jsonObject.get(key).toString();
   }
   
   public static <T> String toJSONString(List<T> list)
   {
     JSONArray jsonArray = JSONArray.fromObject(list);
     
     return jsonArray.toString();
   }
   
   public static String toJSONString(Object object)
   {
     JSONArray jsonArray = JSONArray.fromObject(object);
     
     return jsonArray.toString();
   }
   
   public static String toJSONString(JSONArray jsonArray)
   {
     return jsonArray.toString();
   }
   
   public static String toJSONString(JSONObject jsonObject)
   {
     return jsonObject.toString();
   }
   
   public static List toArrayList(Object object)
   {
     List arrayList = new ArrayList();
     
     JSONArray jsonArray = JSONArray.fromObject(object);
     
     Iterator it = jsonArray.iterator();
     while (it.hasNext())
     {
       JSONObject jsonObject = (JSONObject)it.next();
       
       Iterator keys = jsonObject.keys();
       while (keys.hasNext())
       {
         Object key = keys.next();
         Object value = jsonObject.get(key);
         arrayList.add(value);
       }
     }
     return arrayList;
   }
   
   public static JSONArray toJSONArray(Object object)
   {
     return JSONArray.fromObject(object);
   }
   
   public static JSONObject toJSONObject(Object object)
   {
     return JSONObject.fromObject(object);
   }
   
   public static HashMap toHashMap(Object object)
   {
     HashMap<String, Object> data = new HashMap();
     JSONObject jsonObject = toJSONObject(object);
     Iterator it = jsonObject.keys();
     while (it.hasNext())
     {
       String key = String.valueOf(it.next());
       Object value = jsonObject.get(key);
       data.put(key, value);
     }
     return data;
   }
   
   public static List<Map<String, Object>> toList(Object object)
   {
     List<Map<String, Object>> list = new ArrayList();
     JSONArray jsonArray = JSONArray.fromObject(object);
     for (Object obj : jsonArray)
     {
       JSONObject jsonObject = (JSONObject)obj;
       Map<String, Object> map = new HashMap();
       Iterator it = jsonObject.keys();
       while (it.hasNext())
       {
         String key = (String)it.next();
         Object value = jsonObject.get(key);
         map.put(key, value);
       }
       list.add(map);
     }
     return list;
   }
   
   public static <T> List<T> toList(JSONArray jsonArray, Class<T> objectClass)
   {
     return JSONArray.toList(jsonArray, objectClass);
   }
   
   public static <T> List<T> toList(Object object, Class<T> objectClass)
   {
     JSONArray jsonArray = JSONArray.fromObject(object);
     
     return JSONArray.toList(jsonArray, objectClass);
   }
   
   public static <T> T toBean(JSONObject jsonObject, Class<T> beanClass)
   {
     return (T) JSONObject.toBean(jsonObject, beanClass);
   }
   
   public static <T> T toBean(Object object, Class<T> beanClass)
   {
     JSONObject jsonObject = JSONObject.fromObject(object);
     
     return (T) JSONObject.toBean(jsonObject, beanClass);
   }
   
   public static void main(String[] args) {}
 }



/* Location:           D:\repository\cc\hwtech\core\2.0.1-school-SNAPSHOT\core-2.0.1-school-SNAPSHOT.jar

 * Qualified Name:     JsonHelper

 * JD-Core Version:    0.7.0.1

 */
